package com.han.fu.api.business.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.han.fu.api.business.entity.Comment;
import com.han.fu.api.business.entity.User;
import com.han.fu.api.business.mapper.CommentMapper;
import com.han.fu.api.business.model.ao.CommentAddAo;
import com.han.fu.api.business.model.ao.CommentDeleteAo;
import com.han.fu.api.business.model.ao.CommentPageAo;
import com.han.fu.api.business.model.vo.CommentInfoVo;
import com.han.fu.api.business.model.vo.CommentVo;
import com.han.fu.api.business.service.ICommentService;
import com.han.fu.api.business.service.IUserService;
import com.han.fu.api.common.exception.BusinessException;
import com.han.fu.api.common.model.UserDetail;
import com.han.fu.api.common.utils.CurrentUserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 评论接口实现类
 *
 * @author Star.zhu
 * @since 2020-03-26
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    IUserService userService;
    @Autowired
    CurrentUserUtil currentUserUtil;

    /**
     * 评论分页查询
     *
     * @param ao 查询条件
     * @return Page<CommentVo> 分页数据
     */
    @Override
    public Page<CommentVo> queryCommentPage(CommentPageAo ao) {
        Page<CommentVo> page;
        if (StringUtils.isNotBlank(ao.getReplyId())) {
            page = baseMapper.queryCommentReplyPage(ao);
        } else {
            page = baseMapper.queryCommentNewsPage(ao);
        }
        if (Objects.nonNull(page) && CollectionUtils.isNotEmpty(page.getRecords())) {
            for (CommentVo record : page.getRecords()) {
                User user = userService.getById(record.getCreateUser());
                record.setHeaderName(user.getHeaderName());
                record.setCreateUserName(user.getCreateUserName());
                record.setAuthentication(user.getAuthentication());
            }
        }
        return page;
    }

    /**
     * 评论明细查询
     *
     * @param id 明细ID
     * @return
     */
    @Override
    public CommentInfoVo queryCommentDetail(String id) {
        CommentInfoVo commentInfoVo = new CommentInfoVo();
        Comment comment = super.getById(id);
        if (null != comment) {
            BeanUtils.copyProperties(comment, commentInfoVo);
        }
        return commentInfoVo;
    }

    /**
     * 评论新增数据
     *
     * @param ao 数据
     */
    @Override
    public void addComment(CommentAddAo ao) {
        Comment entity = new Comment();
        entity.setContent(ao.getContent());
        entity.setNewsId(ao.getNewsId());
        entity.setReplyId(ao.getReplyId());
        super.save(entity);
    }

    /**
     * 评论删除数据
     *
     * @param ao 数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteComment(CommentDeleteAo ao) {
        UserDetail currentUser = currentUserUtil.getUser();
        Comment entity = new Comment();
        entity.setId(ao.getId());
        Comment comment = super.getById(ao.getId());
        if (Objects.nonNull(comment) && currentUser.getId().equals(comment.getId())) {
            super.removeById(entity.getId());
        } else {
            throw new BusinessException("无权限操作数据");
        }
    }

}